package org.sang.service;

import org.sang.bean.MbrCard;
import org.sang.bean.MbrCardExample;
import org.sang.bean.RechargeRecord;
import org.sang.bean.RechargeRecordExample;
import org.sang.common.SnowflakeIdGeneratorUtils;
import org.sang.common.UserUtils;
import org.sang.exception.ConsumeException;
import org.sang.mapper.ext.MbrCardExtMapper;
import org.sang.mapper.ext.RechargeRecordExtMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * Description: 会员卡操作业务 .<br>
 *
 * @author jm
 *         Created By 2018/4/4 下午1:41
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RechargeRecordService {
    @Autowired

    private RechargeRecordExtMapper rechargeRecordExtMapper;
    @Autowired
    private MbrCardExtMapper mbrCardExtMapper;


    public int addRechargeRecord(RechargeRecord rechargeRecord) {
        //检验会员卡是否存在
        MbrCard mbrCard = new MbrCard();
        mbrCard.setStoreId(rechargeRecord.getStoreId());
        mbrCard.setCardNo(rechargeRecord.getCardNo());

        MbrCardExample mbrCardExample = new MbrCardExample();
        mbrCardExample.createCriteria().andStoreIdEqualTo(rechargeRecord.getStoreId())
            .andCardNoEqualTo(rechargeRecord.getCardNo());
        List<MbrCard> mbrCardList = mbrCardExtMapper.selectByExample(mbrCardExample);
        if (CollectionUtils.isEmpty(mbrCardList)) {
            throw new ConsumeException("会员卡不存在");
        }
        MbrCard mbrCardTemp = mbrCardList.get(0);

        if ("0".equals(mbrCardTemp.getState())) {
            throw new ConsumeException("会员卡无效");
        }
        try {
            rechargeRecord.setId(SnowflakeIdGeneratorUtils.getId());

            rechargeRecord.setRechargeTime(new Date());

            rechargeRecord.setCreatedTime(new Date());
            rechargeRecord.setUpdateTime(new Date());
            rechargeRecord.setCreatedOperator(UserUtils.getCurrentUser().getUsername());
            rechargeRecord.setUpdateOperator(UserUtils.getCurrentUser().getUsername());

            rechargeRecordExtMapper.insert(rechargeRecord);
            //修改余额
            int newBalance = rechargeRecord.getBalance() + mbrCardTemp.getBalance();
            mbrCardTemp.setBalance(newBalance);
            mbrCardTemp.setUpdateOperator(UserUtils.getCurrentUser().getUsername());
            mbrCardTemp.setUpdateTime(new Date());
            mbrCardExtMapper.updateBalance(mbrCardTemp);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ConsumeException("新增充值记录异常");
        }
    }


    /**
     * 修改.
     *
     * @param rechargeRecord
     * @return
     */
    public int updateRechargeRecord(RechargeRecord rechargeRecord) {
        rechargeRecord.setUpdateTime(new Date());
        rechargeRecord.setUpdateOperator(UserUtils.getCurrentUser().getUsername());

        RechargeRecordExample rechargeRecordExample = new RechargeRecordExample();
        rechargeRecordExample.createCriteria().andIdEqualTo(rechargeRecord.getId());
        return rechargeRecordExtMapper.updateByExampleSelective(rechargeRecord, rechargeRecordExample);
    }

    /**
     * 删除.
     */
    public int deleteRechargeRecord(String id) {

        RechargeRecordExample rechargeRecordExample = new RechargeRecordExample();
        rechargeRecordExample.createCriteria().andIdEqualTo(id);
        return rechargeRecordExtMapper.deleteByExample(rechargeRecordExample);

    }

    /**
     * 查找单个.
     */
    public RechargeRecord queryRechargeRecord(String id) {

        RechargeRecordExample rechargeRecordExample = new RechargeRecordExample();
        rechargeRecordExample.createCriteria().andIdEqualTo(id);
        List<RechargeRecord> rechargeRecords = rechargeRecordExtMapper.selectByExample(rechargeRecordExample);
        if (CollectionUtils.isEmpty(rechargeRecords)) {
            return null;
        } else {
            return rechargeRecords.get(0);
        }

    }

    /**
     * 按照门店编号查找列表.
     */
    public List<RechargeRecord> queryRechargeRecords(RechargeRecord rechargeRecord) {

        RechargeRecordExample rechargeRecordExample = new RechargeRecordExample();
        rechargeRecordExample.createCriteria().andStoreIdEqualTo(rechargeRecord.getStoreId());
        return rechargeRecordExtMapper.selectByExample(rechargeRecordExample);

    }
}
